Shader "Unlit/PlanarReflection"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _BlurOffset("BlurOffset",Range(0,7)) = 0.01
        _ReflectColor("ReflectColor",Color) = (1,1,1,1)
    }
    SubShader
    {
        Tags { 
            "RenderType"="Transparent"
            "RenderPipeline"="UniversalRenderPipeline"
         }

        Blend SrcAlpha OneMinusSrcAlpha

        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            float4 _MainTex_ST;
            float4 _ReflectColor;
            sampler2D _ReflectRT;
            float4 _ReflectRT_TexelSize;//每个纹素texel大小

            half4 KawaseBlur(float2 uv, half4 pixelOffset)//像素偏移
            {
                half4 o = 0;
                o += tex2D(_MainTex, uv + float2(pixelOffset+0.5, pixelOffset+0.5)*_ReflectRT_TexelSize);
                o += tex2D(_MainTex, uv + float2(-pixelOffset - 0.5, pixelOffset+0.5)*_ReflectRT_TexelSize);
                o += tex2D(_MainTex, uv + float2(-pixelOffset -0.5, -pixelOffset- 0.5)*_ReflectRT_TexelSize);
                o += tex2D(_MainTex, uv + float2(pixelOffset+0.5, - pixelOffset- 0.5)*_ReflectRT_TexelSize);
            }

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //RT的值在编辑器调低
                //和空气扰动是一样的？顶点/投影
               float2 normalizedScreenUV = i.vertex.xy / _ScreenParams.xy;
                fixed4 col = KawaseBlur( normalizedScreenUV,_BlurOffset);
                // fixed4 col = tex2D(_ReflectRT, normalizedScreenUV);
                return col*_ReflectColor;
            }
            ENDCG
        }
    }
}
